package com.rjwh.dingdong.client.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsLog;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.FlowTagAdapter;
import com.rjwh.dingdong.client.bean.localbean.FilterItem;
import com.rjwh.dingdong.client.bean.localbean.TagFilter;
import com.rjwh.dingdong.client.widget.FlowTagLayout;
import com.rjwh.dingdong.client.widget.OnTagSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 帖子标签view
 *
 * @author 王小珂
 * @ClassName TagLayout
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2016-4-28 下午5:47:41
 * @history 1.YYYY-MM-DD author: description:
 */
public class PublishTopicTagView extends LinearLayout {
    private OnFlowTagViewListener listener;
    protected String currentTagId = "";
    private TextView tagViewRootName;
    private FlowTagLayout tagLayout;
    protected String currentName = "";

    public void setOnTopicTagViewListener(OnFlowTagViewListener listener) {
        this.listener = listener;
    }

    public PublishTopicTagView(Context context) {
        super(context);
        init();
    }

    public PublishTopicTagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        final View view = inflate(getContext(),
                R.layout.community_publish_topic_tagview_root, null);
        addView(view);
        tagViewRootName = (TextView) view
                .findViewById(R.id.act_community_publish_topic_tagview_name);
        tagLayout = (FlowTagLayout) view
                .findViewById(R.id.act_community_publish_topic_tagview_flowtag);
    }

    public void setTagData(final TagFilter filter) {
        String multiple = filter.getMultiple();
        if (!AsStrUtils.isEmpty(multiple)) {
            if (multiple.equals("0")) {
                if (!AsStrUtils.isEmpty(filter.getName())) {
                    tagViewRootName.setText(filter.getName());
                }
                addSingleCheckTag(filter.getItems());
            } else {
                if (!AsStrUtils.isEmpty(filter.getName())) {
                    tagViewRootName.setText(filter.getName() + "(可多选)");
                }
                addMultiCheckTag(filter.getItems());
            }
        }
    }

    private void addMultiCheckTag(List<FilterItem> list) {
        // TODO 设置多选tag
        List<FilterItem> filterItems = new ArrayList<FilterItem>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getActive().equals("0")) {
                filterItems.add(list.get(i));
            }
        }
        FlowTagAdapter multiTagAdapter = new FlowTagAdapter(getContext());
        tagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
        tagLayout.setAdapter(multiTagAdapter);
        multiTagAdapter.appendToList(filterItems);
        tagLayout.setOnTagSelectListener(new OnTagSelectListener() {

            @Override
            public void onItemSelect(FlowTagLayout parent,
                                     List<Integer> selectedList) {
                if (selectedList != null && selectedList.size() > 0) {
                    StringBuilder sb = new StringBuilder();
                    StringBuilder sbName = new StringBuilder();

                    for (int i = 0; i < selectedList.size(); i++) {
                        FilterItem item = (FilterItem) parent.getAdapter()
                                .getItem(selectedList.get(i));
                        String id = item.getId();
                        String name = item.getName();
                        AsLog.e("PublishTopicTagView", "id: " + id);
                        sb.append(id);
                        sbName.append(name);
                        if (i + 1 < selectedList.size()) {
                            sb.append(",");
                            sbName.append(",");
                        }
                    }
                    currentTagId = sb.toString();
                    currentName = sbName.toString();
                } else {
                    currentTagId = "";
                    currentName = "";
                }
                if (listener != null) {
                    listener.setTagCheck(PublishTopicTagView.this);
                }
                AsLog.e("PublishTopicTagView", "id: " + currentTagId
                        + "selectedList: " + "name: " + currentName);
            }
        });
    }

    private void addSingleCheckTag(List<FilterItem> list) {
        List<FilterItem> filterItems = new ArrayList<FilterItem>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getActive().equals("0")) {
                filterItems.add(list.get(i));
            }
        }
        FlowTagAdapter singleTagAdapter = new FlowTagAdapter(getContext());
        tagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        tagLayout.setAdapter(singleTagAdapter);
        singleTagAdapter.appendToList(filterItems);
        tagLayout.setOnTagSelectListener(new OnTagSelectListener() {

            @Override
            public void onItemSelect(FlowTagLayout parent,
                                     List<Integer> selectedList) {
                if (selectedList != null && selectedList.size() > 0) {
                    for (int i = 0; i < selectedList.size(); i++) {
                        FilterItem item = (FilterItem) parent.getAdapter()
                                .getItem(selectedList.get(i));
                        String id = item.getId();
                        String name = item.getName();
                        currentTagId = id;
                        currentName = name;
                    }
                } else {
                    currentTagId = "";
                    currentName = "";
                }
                if (listener != null) {
                    listener.setTagCheck(PublishTopicTagView.this);
                }
                AsLog.e("PublishTopicTagView", "id: " + currentTagId
                        + "selectedList: " + "name: " + currentName);
            }
        });

    }

    public String getSubid() {
        return currentTagId;
    }

    public interface OnFlowTagViewListener {
        /**
         * 设置Tag选中
         *
         * @param @param view
         * @return void
         * @throws
         * @Title: setTagCheck
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @history 1.YYYY-MM-DD author: description:
         */
        void setTagCheck(PublishTopicTagView view);
    }

}
